package zozopo.background.operator;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.background.operator.operations.DepartmentOS;
import zozopo.background.operator.operations.DepartmentTB;
import zozopo.background.operator.operations.OperatorOS;
import zozopo.background.operator.operations.OperatorTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class PreModifyOperatorInfoAction extends ActionSupport implements Action,SessionAware{
	
	//传入参数
	private String email = "";
	//传出参数
	private OperatorTB operator;
	
	private List<DepartmentTB> departmentList;
	private LinkedHashMap<Integer,DepartmentTB> departmentMap;
	
	
	private Map<Object, Object> session;
	
	
	
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		
		
		
		session = arg0;
	}
	
	public String execute(){
		OperatorOS operatorOS = new OperatorOS();
		DepartmentOS departmentOS = new DepartmentOS(operatorOS.getConn());
		operator = operatorOS.getInfoByEmail(email);
		
		departmentList = departmentOS.select();
		departmentMap = new  LinkedHashMap<Integer,DepartmentTB>();
		
		getDepartmentMap(0);
		
		operatorOS.releaseConnection();
		return SUCCESS;
	}
	
	public void validate() {
		//权限处理
		//UserTB user = (UserTB) session.get("userInfo");
		
		operator = (OperatorTB)session.get("operatorInfo"); 

		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		if(!OperatorOS.operatorFlagLeve(operator, 17, 1)){
			
			addFieldError("errormessage","权限不足");
			return ; 
		}//*/
		
	
		
	}

	
	//排例
	public LinkedHashMap<Integer,DepartmentTB>  getDepartmentMap(int parentid){
		
		for(DepartmentTB department:departmentList){
			if(!departmentMap.containsKey(department.getId_department())){//如果已不存在
				if(department.getParentid_department() == parentid){//如果父ID正常
					departmentMap.put(department.getId_department(), department); 
					getDepartmentMap(department.getId_department());
				}
			}
		}

		return departmentMap;
	}
	
	public OperatorTB getOperator() {
		return operator;
	}

	public void setOperator(OperatorTB operator) {
		this.operator = operator;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public List<DepartmentTB> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<DepartmentTB> departmentList) {
		this.departmentList = departmentList;
	}

	public LinkedHashMap<Integer, DepartmentTB> getDepartmentMap() {
		return departmentMap;
	}

	public void setDepartmentMap(LinkedHashMap<Integer, DepartmentTB> departmentMap) {
		this.departmentMap = departmentMap;
	}

}
